package com.oneonefive.educationsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneonefive.educationsystem.domain.Lab;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.mapper.LabMapper;
import com.oneonefive.educationsystem.service.LabService;
import com.oneonefive.educationsystem.utils.StateChangeUtil;
import com.oneonefive.educationsystem.vo.LabVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class LabServiceImpl extends ServiceImpl<LabMapper, Lab>
        implements LabService {

    @Resource
    LabMapper labMapper;

    @Override
    public Result labList(LabVo labVo) {
        List<Lab> labs = labMapper.labListCount(labVo);
        long size = labMapper.labListCount(labVo).size();
        if (labVo.getPage() != null) {
            labVo.setPage((labVo.getPage() - 1) * 10);
             labs = labMapper.labList(labVo);
        }
        List<LabVo> resList = getResVo(labs);
        return Result.ok(resList, size);
    }

    public List<LabVo> getResVo(List<Lab> list) {
        List<LabVo> resList = new ArrayList<>();
        for (Lab lab : list) {
            LabVo labVo = new LabVo();
            BeanUtils.copyProperties(lab, labVo);
            labVo.setLabTypeString(StateChangeUtil.labTypeChange(lab.getLabType()));
            resList.add(labVo);
        }
        return resList;
    }
    @Override
    public Result legitimacy(Lab lab) {
        if (lab.getLabId().length() > 10) {
            return Result.fail("实验室编号过长");
        }
        if (lab.getLabName().length() > 10) {
            return Result.fail("实验室名字过长");
        }

        if (lab.getLabType() < 0 || lab.getLabType() > 2) {
            return Result.fail("实验室类型不存在");
        }
        if (lab.getEquipment() < 0) {
            return Result.fail("实验室人数不为负");
        }

        return Result.ok();
    }

    @Override
    public Result getByLabId(String labId) {
        Lab lab = labMapper.getByLabId(labId);
        if (lab != null) {
            return Result.ok(lab);
        }
        return Result.fail("lab不存在");
    }

    @Override
    public Result getTypeList(Integer type) {
        QueryWrapper<Lab> wrapper = new QueryWrapper<>();
        wrapper.eq("lab_type", type);
        List<Lab> labs = labMapper.selectList(wrapper);
        return Result.ok(labs);
    }
}




